﻿Imports System.Data.SqlClient
Imports System.IO
Imports extranet.BE
Imports MySql.Data.MySqlClient

Public Class DAVideo
  Dim objBD As New BancoDeDados
  Dim objDS As New DataSet
  Public objQuery As New BEQuery

  Sub New()
  End Sub

  Sub New(ByVal booCondicaoSimilar As Boolean)
    objQuery.Where.CondicaoSimilar = booCondicaoSimilar
  End Sub

  Public Sub Cadastra(ByRef objBEVideo As BEVideo)
    Try
      With objBEVideo
        .IdVideo = _
        objBD.ExecutaEscalar("INSERT INTO ext_videos (video, titulo, descricao, id_categoria, " & _
                                                      "usuario, width, height) " & _
                              "VALUES ('" & .Video & "', '" & _
                                            .Titulo & "', '" & _
                                            .Descricao & "', '" & _
                                            .Categoria.IdCategoria & "', '" & _
                                            .Usuario.Usuario & "', '" & _
                                            .Width & "', '" & _
                                            .Height & "');")
      End With
    Catch ex As ApplicationException
      Throw New ApplicationException("extranet.DA.DAVideo.Cadastra(): " & ex.Message)
    End Try
  End Sub

  Public Sub Atualiza(ByVal objBEVideo As BEVideo)
    Try
      With objBEVideo
        objBD.ExecutaNonQuery("UPDATE ext_videos " & _
                              "SET video='" & .Video & "', " & _
                                  "imagem='" & .Imagem & "', " & _
                                  "titulo='" & .Titulo & "', " & _
                                  "descricao='" & .Descricao & "', " & _
                                  "id_categoria='" & .Categoria.IdCategoria & "', " & _
                                  "usuario='" & .Usuario.Usuario & "', " & _
                                  "width='" & .Width & "', " & _
                                  "height='" & .Height & "' " & _
                              "WHERE id_video=" & .IdVideo & ";")
      End With
    Catch ex As ApplicationException
      Throw New ApplicationException("extranet.DA.DAVideo.Atualiza(): " & ex.Message)
    End Try
  End Sub

  Public Sub AtualizaImagem(ByVal objBEVideo As BEVideo)
    Try
      With objBEVideo
        objBD.ExecutaNonQuery("UPDATE ext_videos " & _
                              "SET imagem='" & .Imagem & "' " & _
                              "WHERE id_video=" & .IdVideo & ";")
      End With
    Catch ex As ApplicationException
      Throw New ApplicationException("extranet.DA.DAVideo.AtualizaImagem(): " & ex.Message)
    End Try
  End Sub

  Public Sub Exclui(ByVal objBEVideo As BEVideo)
    Try
      ' Excluir do Banco de Dados
      objBD.ExecutaNonQuery("DELETE FROM ext_videos WHERE id_video=" & objBEVideo.IdVideo & ";")
      ' Ler Diretório atual
      Dim strDiretorio As String = objBEVideo.Diretorio
      If Not strDiretorio.EndsWith("\") Then strDiretorio &= "\"
      ' Ler Registro
      objBEVideo = SelecionaVideo(objBEVideo)
      ' Apagar Arquivos: StartImage, ThumbNail Image, Xml File, Video
      If File.Exists(strDiretorio & "startImage\" & objBEVideo.Video & ".jpg") Then File.Delete(strDiretorio & "startImage\" & objBEVideo.Video & ".jpg")
      If File.Exists(strDiretorio & "thumb\" & objBEVideo.Video & ".jpg") Then File.Delete(strDiretorio & "thumb\" & objBEVideo.Video & ".jpg")
      If File.Exists(strDiretorio & "xml\" & objBEVideo.Video & ".xml") Then File.Delete(strDiretorio & "xml\" & objBEVideo.Video & ".xml")
      If File.Exists(strDiretorio & "video\" & objBEVideo.Video & ".xml") Then File.Delete(strDiretorio & "video\" & objBEVideo.Video & ".xml")
      If File.Exists(strDiretorio & "video\" & objBEVideo.Video) Then File.Delete(strDiretorio & "video\" & objBEVideo.Video)
    Catch ex As ApplicationException
      Throw New ApplicationException("extranet.DA.DAVideo.Exclui(): " & ex.Message)
    End Try
  End Sub

  Public Function SelecionaVideo(ByVal objBEVideo As BEVideo) As BEVideo
    Try
      objQuery.Where.CondicaoSimilar = False
      Dim objBEVideos As List(Of BEVideo) = SelecionaVideos(objBEVideo)
      If objBEVideos.Count = 1 Then
        objBEVideo = objBEVideos(0)
      Else
        objBEVideo = Nothing
      End If
    Catch ex As ApplicationException
      Throw New ApplicationException("extranet.DA.DAVideo.SelecionaVideo(): " & ex.Message)
    End Try
    Return objBEVideo
  End Function

  Public Function SelecionaVideos(ByVal objBEVideo As BEVideo) As List(Of BEVideo)
    Dim objBEVideos As List(Of BEVideo) = New List(Of BEVideo)
    Dim arrFields() As String = {"v.titulo", "v.descricao"}
    Try
      objBD.duplicaAspasSimples(objBEVideo, GetType(BEVideo))
      objQuery._Select = "SELECT v.*, cv.categoria FROM ext_videos v " & _
                         "INNER JOIN ext_categoria_videos cv ON v.id_categoria=cv.id_categoria"
      If objBEVideo IsNot Nothing Then
        If objBEVideo.IdVideo > 0 Then objQuery.Where.AdicionarCondicaoWhere("v.id_video", objBEVideo.IdVideo, False)
        objQuery.Where.AdicionarCondicaoWhere(arrFields, objBEVideo.Titulo)
        If objBEVideo.Categoria IsNot Nothing Then
          If objBEVideo.Categoria.IdCategoria > 0 Then _
             objQuery.Where.AdicionarCondicaoWhere("v.id_categoria", objBEVideo.Categoria.IdCategoria, False)
          objQuery.Where.AdicionarCondicaoWhere("cv.categoria", objBEVideo.Categoria.Categoria)
        End If
        If objBEVideo.Usuario.Usuario.Length > 0 Then _
           objQuery.Where.AdicionarCondicaoWhere("usuario", objBEVideo.Usuario.Usuario, False)
      End If
      objBEVideos = SelecionaVideos()
    Catch ex As ApplicationException
      Throw New ApplicationException("extranet.DA.DAVideo.SelecionaVideos(): " & ex.Message)
    End Try
    Return objBEVideos
  End Function

  Public Function SelecionaUltimoVideo() As BEVideo
    Dim objBEVideo As BEVideo = Nothing
    Dim arrFields() As String = {"v.titulo", "v.descricao"}
    Try
      objQuery._Select = "SELECT v.*, cv.categoria FROM ext_videos v " & _
                         "INNER JOIN ext_categoria_videos cv ON v.id_categoria=cv.id_categoria " & _
                         "ORDER BY v.id_video DESC LIMIT 1"
      Dim objBEVideos As List(Of BEVideo) = SelecionaVideos()
      If objBEVideos.Count = 1 Then objBEVideo = objBEVideos(0)
    Catch ex As ApplicationException
      Throw New ApplicationException("extranet.DA.DAVideo.SelecionaVideos(): " & ex.Message)
    End Try
    Return objBEVideo
  End Function

  Private Function SelecionaVideos() As List(Of BEVideo)
    Dim objBEVideos As List(Of BEVideo) = New List(Of BEVideo)
    Dim objDACategoriaVideo As New DACategoriaVideo()
    Try
      Using objSQLCommand As MySqlCommand = New MySqlCommand(objQuery.Query, BancoDeDados.ConnectionInstance())
        Using objSQLDReader As MySqlDataReader = objSQLCommand.ExecuteReader()
          While objSQLDReader.Read()
            Dim objBEVideo As New BEVideo()
            With objBEVideo
              .IdVideo = objSQLDReader("id_video")
              .Video = objSQLDReader("video").ToString()
              .Imagem = objSQLDReader("imagem").ToString()
              .Titulo = objSQLDReader("titulo").ToString()
              .Descricao = objSQLDReader("descricao").ToString()
              .Categoria = New BECategoriaVideo(objSQLDReader("id_categoria"), objSQLDReader("categoria"))
              .Ranking = objSQLDReader("ranking")
              .Usuario = New BEUsuario(objSQLDReader("usuario").ToString(), "")
              .Width = objSQLDReader("width")
              .Height = objSQLDReader("height")
            End With
            objBEVideos.Add(objBEVideo)
          End While
        End Using
      End Using
    Catch ex As ApplicationException
      Throw New ApplicationException("extranet.DA.DAVideo.SelecionaVideos(): " & ex.Message)
    End Try
    Return objBEVideos
  End Function

  Public Function Ranking(ByVal objBEVideo As BEVideo) As Boolean
    Try
      objBD.ExecutaNonQuery("UPDATE ext_videos " & _
                            "SET ranking=ranking+1 " & _
                            "WHERE id_video=" & objBEVideo.IdVideo & ";")
      Return True
    Catch ex As ApplicationException
      Throw New ApplicationException("extranet.DA.DAVideo.Ranking(): " & ex.Message)
      Return False
    End Try
  End Function

End Class
